import { Pipe, PipeTransform } from "@angular/core";

@Pipe({
  name: "unitPipe",
})
export class UnitPipe implements PipeTransform {
  transform(value, unitParam) {
    if (value === undefined) return "";
    let unitPos = unitParam || 0;
    enum unitList {"", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿"};
    let unit = (isNaN(parseInt(unitList[unitPos]))) ? unitList[unitPos] : unitList[unitList[unitPos]];
    return (value / Math.pow(10, parseInt(unitList[unit]))).toFixed(2) + unit.toString();
  }
}
